#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>


struct ListNode {
    int val;
    struct ListNode* next;
};

typedef struct ListNode LNode;
struct ListNode* detectCycle(struct ListNode* head) {
    if (head == NULL)
    {
        return NULL;
    }
    LNode* fast = head;
    LNode* slow = head;
    while (fast && fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (fast == slow)
        {
            LNode* in = fast;
            slow = head;
            while (slow != in)
            {
                slow = slow->next;
                in = in->next;
            }
            return in;
        }
    }
    return NULL;
}